<template>
  <div
    class="time-container"
    :class="{ dark: isDark }"
    @click="toggleTheme"
    :date="date"
  >
    <div class="time">
      <template v-for="(str, idx) in time">
        <template v-if="str !== ':'">
          <div class="time-num" :style="numStyle[idx]" :key="idx">
            <span v-for="(i, spanIdx) in numSpans[idx]" :key="spanIdx">{{
              i - 1
            }}</span>
            <span>0</span>
          </div>
        </template>
        <template v-else>
          <div class="time-dist" :key="idx">
            <span>{{ str }}</span>
          </div>
        </template>
      </template>
    </div>
  </div>
</template>
<script>
import getTime from '../utils/time'
import { onMounted, reactive, toRefs } from 'vue'
export default {
  name: '',
  setup() {
    let { time, date } = getTime()
    //每个数字容器无动画样式（清除动画），
    const style = 'transform: translateY(0%);transition:0s all'
    //给每个数字容器添加初始动画样式
    const initNumStyle = (val) => {
      return time.split('').map((val) => (val == '0' ? style : setStyle(val)))
    }
    //设置样式
    const setStyle = (val) => {
      return `transform: translateY(-${~~val * 100}%)`
    }
    //清除动画
    const removeAnimate = (idx) => {
      setTimeout(() => {
        data.numStyle[idx] = style
      }, 500)
    }
    //更新样式
    const updateStyle = () => {
      data.time.split('').map((val, i) => {
        if (val == 0) {
          //无缝连接
          if (data.numStyle[i] != style) {
            data.numStyle[i] = setStyle(data.numSpans[i])
            removeAnimate(i)
          }
        } else {
          data.numStyle[i] = setStyle(val)
        }
      })
    }
    // 每秒更新时间
    const updateTime = () => {
      const { time, date } = getTime()
      data.time = time
      data.date = date
      updateStyle()
    }
    // 切换主题
    const toggleTheme = () => {
      data.isDark = !data.isDark
    }
    let data = reactive({
      isDark: 0,
      timer: null,
      time,
      date,
      numSpans: [3, 10, 0, 6, 10, 0, 6, 10], //每个时间数字有多少个span
      numStyle: initNumStyle(time) //容器动画样式
    })
    onMounted(() => {
      data.timer = setInterval(() => {
        updateTime()
      }, 1000)
    })
    return {
      ...toRefs(data),
      toggleTheme
    }
  }
}
</script>

<style lang="scss" scoped>
%flexCenter {
  display: flex;
  justify-content: center;
  align-items: center;
}
$timeColor: #d9d4d0;
$white: #fff;
.time-container {
  background: $white;
  color: $timeColor;
  position: absolute;
  width: 100%;
  height: 100%;
  max-width: 540px;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  &.dark {
    background: #000;
    color: $white;
  }
  &::after {
    content: attr(date);
    position: absolute;
    color: $timeColor;
    font-size: 18px;
    line-height: 1;
    transform: rotate(90deg);
    bottom: 20%;
    left: -48px;
  }
  @extend %flexCenter;
  .time {
    font-size: 70px;
    transform: rotate(90deg);
    position: relative;
    height: 106px;
    line-height: 106px;
    overflow: hidden;
    @extend %flexCenter;

    .time-num {
      position: relative;
      width: 100%;
      height: 100%;
      text-align: center;
      text-shadow: 0 0 2px $white;
      transition: 0.5s all;
      span {
        display: block;
      }
    }
    .time-dist {
      padding-bottom: 15px;
      margin: 0 10px;
    }
  }
}
</style>
